//
//  BookManager.m
//  03_NSStringHomework
//
//  Created by 徐昊 on 16/7/6.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "BookManager.h"

@implementation BookManager

#pragma mark - informationOfBook
- (void)bookInformation:(NSInteger)index {
    Book *book = [self.mArrOfBook objectAtIndex:index];
    NSLog(@"书籍是%@, 价格是%@", book.name, book.price);
}

#pragma mark - add
- (void)addBookName:(NSString *)bookName price:(NSString *)price {
    NSMutableArray *nameArray = [[NSMutableArray alloc] init];
    for (Book *book in self.mArrOfBook) {
        [nameArray addObject:book.name];
    }
    if ([nameArray containsObject:bookName]) {
        NSLog(@"书库里面已经有%@这本书了, 不需要再次添加", bookName);
    } else {
        Book *book = [Book bookWithPrice:price name:bookName];
        [self.mArrOfBook addObject:book];
        NSLog(@"成功添加%@这本书", bookName);
    }
}
#pragma mark - delete
- (void)deleteBookName:(NSString *)bookName {
    NSMutableArray *nameArray = [[NSMutableArray alloc] init];
    for (Book *book in self.mArrOfBook) {
        [nameArray addObject:book.name];
    }
    if ([nameArray containsObject:bookName]) {
        NSInteger index = [nameArray indexOfObject:bookName];
        [self.mArrOfBook removeObjectAtIndex:index];
        NSLog(@"成功删除%@这本书", bookName);
    } else {
        NSLog(@"书库中不存在%@这本书,无法删除",bookName);
    }
}

#pragma mark - search
- (void)searchBook:(NSString *)bookName {
    NSMutableArray *nameArray = [[NSMutableArray alloc] init];
    for (Book *book in self.mArrOfBook) {
        [nameArray addObject:book.name];
    }
    if ([nameArray containsObject:bookName]) {
        NSInteger index = [nameArray indexOfObject:bookName];
        [self bookInformation:index];
    } else {
        NSLog(@"书库里没有%@这本书!", bookName);
    }
    
}

#pragma mark - correct
- (void)correctBook:(NSString *)name price:(NSString *)price {
    if (name != nil) {
        for (Book *book in self.mArrOfBook) {
            if (book.name == name) {
                book.price =price;
                NSLog(@"成功修改%@的价格,现在价格是%@", name, price);
            }
        }
    }
}

#pragma mark - allBooksInformation
- (void)allBooksInformation {
    for (Book *book in self.mArrOfBook) {
        NSLog(@"书籍%@, 价格%@", book.name, book.price);
    }
}

@end
